package com.pinecone.controller;

import java.util.List;
import java.util.Map;


import com.pinecone.common.annotation.CurrentUser;
import com.pinecone.common.exception.MinerException;
import com.pinecone.dto.UserDetailsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.pinecone.entity.LiveTypeEntity;
import com.pinecone.service.LiveTypeService;
import com.pinecone.common.utils.PageUtils;
import com.pinecone.common.utils.Query;
import com.pinecone.common.utils.R;




/**
 * 活动类别
 * 
 * @author hushangjie
 * @email 979783618@qq.com
 * @date 2017-09-27 21:18:16
 */
@RestController
@RequestMapping("live_type")
public class LiveTypeController {
	@Autowired
	private LiveTypeService liveTypeService;



    /**
     * 查询某个机构的直播类别
     * @param orgId
     * @return
     */
	@GetMapping("/list/{orgId}")
    public R list(@PathVariable(value = "orgId",required = true) Integer orgId){
        List<LiveTypeEntity> liveTypeEntities = liveTypeService.queryByOrgId(orgId);
        return R.ok().put("types",liveTypeEntities);
    }
	
	
	/**
	 * 信息
	 */
	@GetMapping("/{liveTypeId}")
	public R info(@PathVariable("liveTypeId") Integer liveTypeId){
		LiveTypeEntity liveType = liveTypeService.queryObject(liveTypeId);
		
		return R.ok().put("liveType", liveType);
	}


    /**
     * 机构管理员添加属于该机构的直播类别
     * @param currentUser
     * @param liveType
     * @return
     */
    @PreAuthorize("hasRole('ROLE_ORGMASTER')")
	@PostMapping("")
	public R save(@CurrentUser UserDetailsDTO currentUser ,@RequestBody @Validated LiveTypeEntity liveType){
        //设置默认属性
        liveType.setOrgId(currentUser.getOrgId());
        liveType.setLiveTypeId(null);
		liveTypeService.save(liveType);
		return R.ok();
	}

    /**
     * 修改菜单类别（只有机构管理员可操作）
     * @param currentUser
     * @param liveType
     * @return
     */
    @PreAuthorize("hasRole('ROLE_ORGMASTER')")
	@PutMapping("")
	public R update(@CurrentUser UserDetailsDTO currentUser, @RequestBody @Validated LiveTypeEntity liveType){
		liveType.setOrgId(currentUser.getOrgId());

        liveTypeService.update(liveType);
		
		return R.ok();
	}

    /**
     * 删除直播类别
     * @param currentUser
     * @param liveTypeId
     * @return
     */
    @PreAuthorize("hasRole('ROLE_ORGMASTER')")
	@DeleteMapping("")
	public R delete(@CurrentUser UserDetailsDTO currentUser, @RequestParam(value = "live_type_id",required = true) Integer liveTypeId){
        //判断该直播类别是否属于该机构
        LiveTypeEntity liveTypeEntity = liveTypeService.queryObject(liveTypeId);
        if (liveTypeEntity.getOrgId() != currentUser.getOrgId()){
            throw new MinerException("无权操作",403);
        }
        liveTypeService.delete(liveTypeId);
		
		return R.ok();
	}
    /**
     * 查询某个机构的直播类别
     * 参数limit&page&orgId
     * @param params
     * @return
     */
	/*@GetMapping("")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		if (params.get("page") != null && params.get("limit")!= null){
			List<LiveTypeEntity> liveTypeList = liveTypeService.queryList(query);
			int total = liveTypeService.queryTotal(query);

			PageUtils pageUtil = new PageUtils(liveTypeList, total, query.getLimit(), query.getPage());

			return R.ok().put("page", pageUtil);
		}else{
			List<LiveTypeEntity> liveTypeList = liveTypeService.queryList(query);
			return R.ok().put("list", liveTypeList);
		}
	}*/
}
